package com.wswy.wzcx.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;

import com.che.libcommon.api.ApiOptionalResultObserver;
import com.che.libcommon.api.BaseResult;
import com.che.libcommon.ui.BaseFragment;
import com.che.libcommon.utils.ToastUtils;
import com.wswy.wzcx.R;
import com.wswy.wzcx.api.Api;
import com.wswy.wzcx.model.DataCenter;
import com.wswy.wzcx.model.UserMode;
import com.wswy.wzcx.model.home.RPaths;
import com.wswy.wzcx.ui.fragment.BookKeepingFragment;
import com.wswy.wzcx.ui.fragment.ChangeUserInfoFragment;
import com.wswy.wzcx.ui.fragment.ListenerFragment;
import com.wswy.wzcx.ui.fragment.MyCarCollectFragment;
import com.wswy.wzcx.ui.fragment.MyOrderFragment;
import com.wswy.wzcx.ui.fragment.SettingFragment;
import com.wswy.wzcx.ui.fragment.ShareFragment;
import com.wswy.wzcx.ui.fragment.UserInfoFragment;
import com.wswy.wzcx.ui.fragment.WyServiceFragment;
import com.wswy.wzcx.ui.message.MessageDetailFragment;
import com.wswy.wzcx.ui.message.MessageFragment;
import com.wswy.wzcx.ui.module.feedback.FeedbackFragment;

/**
 * Created by wangduheng26 on 2018/1/19.
 * wangduheng26@gmail.com
 */

public class MineActivity extends IFragmentActivity implements Toolbar.OnMenuItemClickListener {
    public static Intent asIntent(Context context, String target) {
        Intent intent = new Intent(context, MineActivity.class);
        intent.putExtra(EXTRA_TARGET_NAME, target);
        return intent;
    }

    public static Intent asIntent(Context context, String target, String args) {
        Intent intent = new Intent(context, MineActivity.class);
        intent.putExtra(EXTRA_TARGET_NAME, target);
        intent.putExtra(EXTRA_ARGS_PARAM, args);
        return intent;
    }

    private String currentTag = null;

    protected BaseFragment getFragmentFromExtra(String stringExtra) {
        BaseFragment fragment = null;
        currentTag = stringExtra;
        switch (stringExtra) {
            case SettingFragment.TAG:
                fragment = SettingFragment.newInstance();
                break;
            case MessageFragment.TAG:
                fragment = MessageFragment.newInstance();
                break;
            case MyCarCollectFragment.TAG:
                fragment = MyCarCollectFragment.newInstance();
                break;
            case RPaths.feedback:
                fragment = new FeedbackFragment();
                break;
            case ShareFragment.TAG:
                fragment = ShareFragment.newInstance();
                break;
            case UserInfoFragment.TAG:
                fragment = UserInfoFragment.newInstance();
                break;
            case ChangeUserInfoFragment.TAG:
                fragment = ChangeUserInfoFragment.newInstance();
                break;
            case MessageDetailFragment.TAG:
                fragment = MessageDetailFragment.newInstance(getParam());
                break;
            case ListenerFragment.TAG:
                fragment = ListenerFragment.newInstance();
                break;
            case MyOrderFragment.TAG:
                fragment= MyOrderFragment.newInstance();
                break;
            case BookKeepingFragment.TAG:
                fragment = new BookKeepingFragment();
                break;
            case WyServiceFragment.TAG:
                fragment = new WyServiceFragment();
                break;
        }
        return fragment;

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (TextUtils.equals(currentTag, ChangeUserInfoFragment.TAG)) {
            if (!TextUtils.isEmpty(checkUserInfo())) {
                getMenuInflater().inflate(R.menu.menu_change_user_info, menu);
            }
        }
//        else if (TextUtils.equals(currentTag, MyCarCollectFragment.TAG)) {
//            getMenuInflater().inflate(R.menu.menu_change_my_collect_edit, menu);
//        }
        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_edit_complete:
                save(checkUserInfo());
                break;
            case R.id.action_edit:
                startEditMyCollect();
        }
        return true;
    }

    private void startEditMyCollect() {
        startActivity(null);
    }

    private void save(String s) {
        Api.get().submitUpdateNickName(s)
                .subscribe(new ApiOptionalResultObserver<UserMode>() {
                    @Override
                    protected void onApiResult(@Nullable UserMode userMode) {
                        ToastUtils.showText("改修完成");

                        DataCenter.get().updateUserMode(userMode);
                        finish();
                    }

                    @Override
                    protected void onApiError(@Nullable BaseResult result) {
                        toastErrorMsg(result);
                    }
                });
    }

    private String checkUserInfo() {
        if (fragment instanceof ChangeUserInfoFragment) {
            String text = ((ChangeUserInfoFragment) fragment).getText();
            return text;
        }
        return "";
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    @Override
    public void configToolBar() {
        super.configToolBar();


//        if (TextUtils.equals(currentTag,BookKeepingFragment.TAG)){
//            //养车账本
//            findViewById(R.id.appbar).setVisibility(View.GONE);
//
//        }else {

            toolbar.setOnMenuItemClickListener(this);
            setSupportActionBar(toolbar);
            toolbar.setOnMenuItemClickListener(this);
            final ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayShowTitleEnabled(false);
                actionBar.setDisplayHomeAsUpEnabled(false);
                actionBar.setDisplayShowHomeEnabled(true);
            }
        //}
    }
}
